﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LDTB.Service;

namespace LDTB
{
    public partial class FormServey : Form
    {
        LDTBDataDataContext ds = DataService.DB;
        int uid;
        public FormServey()
        {
            InitializeComponent();
        }
        public FormServey(int id_)
        {

            InitializeComponent();
            uid = id_;
        }

        private void FormServey_Load(object sender, EventArgs e)
        {

            BindingPeopleData();

        }

        private void SearchFilter()
        {

            //lay du lieu ve so kiem tra

            int wardid = ddlWard.SelectedIndex == -1 ? -1 : (int)ddlWard.SelectedValue;
            int villageid = ddlVillage.SelectedIndex == -1 ? -1 : (int)ddlVillage.SelectedValue;
            int groupcatid = ddlGroupCat.SelectedIndex == -1 ? -1 : (int)ddlGroupCat.SelectedValue;
            int catid = ddlCat.SelectedIndex == -1 ? -1 : (int)ddlCat.SelectedValue;

            int uidddl = ddlUser.SelectedIndex == -1 ? -1 : (int)ddlUser.SelectedValue;
            int compare = ddlCompareNumber.SelectedIndex == -1 ? -1 : (int)ddlCompareNumber.SelectedValue;
            string fistname = this.txtName.Text.Trim();
            string faminyname = this.txtFamilyName.Text.Trim();
            int pageindex = this.ddlPage.SelectedIndex == -1 ? 0 : (int)ddlPage.SelectedValue - 1;

            //load du lieu vao form
            var people = from p in ds.Peoples
                         join degree in ds.OptionCates
                         on p.DegreeID equals degree.ID
                         join vll in ds.Villages
                         on p.LocationID equals vll.VillageID
                         where
                         (p.UserID.Value == uidddl || uidddl == -1) &&
                         (vll.WardID.Value == wardid || wardid == -1) &&
                         (p.LocationID==villageid||villageid==-1)&&
                         (degree.ID==catid || catid==-1)&&
                         (degree.CateDisableID.Value==groupcatid||groupcatid==-1)&&
                         (p.FirstName.StartsWith(fistname) || fistname == "") &&
                         (p.FamilyName.StartsWith(faminyname) || faminyname == "") &&
                         (p.NumberPair.Value == (byte)compare || compare == -1) 
                         select new PeopleExtra
                         {
                             ID = p.ID,
                             FamilyName = p.FamilyName,
                             FirstName = p.FirstName,
                             Location = p.Address + "-" + p.Village.VillageName + "-" + p.Village.Ward.WardName,
                             UserName = p.User.FullName,
                             DegreeName = degree.DegreeName,
                             OpjectCate = p.CateObject.CatName,
                             Sex = p.Sex.Value ? "Nam" : "Nữ",
                             ReciveMoneyMonth = p.ReciveMoneyMonth.Value,
                             ReciveMoneyStartDate = p.ReciveMoneyStartDate.Value,
                             NumberPair = p.NumberPair.Value
                         };


            PagedList<PeopleExtra> pl = new PagedList<PeopleExtra>(people, pageindex, 20);

            lbltotal.Text = pl.TotalItemCount.ToString();
            this.dataGridView1.DataSource = pl;

            //thiet lap kich co trang
            List<ItemExtra> ltPage = new List<ItemExtra>();
            for (int i = 1; i < pl.PageCount + 1; i++)
            {
                ltPage.Add(new ItemExtra { ID = i, Name = i.ToString() });
            }
            this.ddlPage.DataSource = ltPage;
            this.ddlPage.DisplayMember = "Name";
            this.ddlPage.ValueMember = "ID";


        }

        private void BindingPeopleData()
        {

            //load du lieu nguoi gui bai
            List<ItemExtra> ltuser = new List<ItemExtra>();
            ltuser.Add(new ItemExtra { ID = -1, Name = "Tất cả" });
            var user = from u in ds.Users
                       select new ItemExtra { ID = u.ID, Name = u.FullName };
            ltuser.AddRange(user.ToList());
            ddlUser.DataSource = ltuser;
            ddlUser.DisplayMember = "Name";
            ddlUser.ValueMember = "ID";


            //kiem tra admin thi chọn người dùng nếu kô thì combo này bị disable và chỉ hiện thị người này
            bool curentUserIsAdmin = (from u in ds.Users
                                      where u.ID == uid
                                      select u).First().IsAdmin.Value;

            if (!curentUserIsAdmin)
            {
                ddlUser.SelectedValue = uid;
                ddlUser.Enabled = false;
            }

            //load ward

            List<Ward> ltward = new List<Ward>();
            ltward.Add(new Ward { ID = -1, WardName = "Tất cả" });
            var ward = from w in ds.Wards
                       select w;
            ltward.AddRange(ward.ToList());
            this.ddlWard.DataSource = ltward;
            this.ddlWard.ValueMember = "ID";
            this.ddlWard.DisplayMember = "WardName";

            //khoi tao Nhóm đối tượng
            List<ItemExtra> ltGroupCat = new List<ItemExtra>();
            ltGroupCat.Add(new ItemExtra { ID = -1, Name = "Tất cả" });
            var grcat = from gr in ds.CateObjects
                        select new ItemExtra { ID = gr.CatID, Name = gr.CatName };
            ltGroupCat.AddRange(grcat.ToList());
            ddlGroupCat.DataSource = ltGroupCat;
            ddlGroupCat.DisplayMember = "Name";
            ddlGroupCat.ValueMember = "ID";
                                       
            //khoi tao đối tượng
            List<ItemExtra> cats = new List<ItemExtra>();
            cats.Add(new ItemExtra { ID = -1, Name = "Tất cả" });
            var cat = from gr in ds.OptionCates
                        select new ItemExtra { ID = gr.ID, Name = gr.DegreeName };

            cats.AddRange(cat.ToList());
            ddlCat.DataSource = cats;
            ddlCat.DisplayMember = "Name";
            ddlCat.ValueMember = "ID";

            //khoi tao lang xa
            int wardid = (int)this.ddlWard.SelectedValue;

            List<ItemExtra> ltvillage = new List<ItemExtra>();
            ltvillage.Add(new ItemExtra { ID=-1,Name="Tất cả"});
            var village = from v in ds.Villages
                          where v.WardID == wardid
                          select new ItemExtra { ID=v.VillageID,Name=v.VillageName};
            ltvillage.AddRange(village.ToList());
            this.ddlVillage.DataSource = ltvillage;
            this.ddlVillage.ValueMember = "ID";
            this.ddlVillage.DisplayMember = "Name";
            this.ddlVillage.SelectedIndex = 0;

            //load cap so

            List<ItemExtra> ltCompa = new List<ItemExtra>();
            ltCompa.Add(new ItemExtra { ID = -1, Name = "Tất cả" });
            for (int i = 1; i < 100; i++)
            {
                ltCompa.Add(new ItemExtra { ID = i, Name = i < 10 ? "0" + i.ToString() : i.ToString() });
            }
            ddlCompareNumber.DataSource = ltCompa;
            ddlCompareNumber.DisplayMember = "Name";
            ddlCompareNumber.ValueMember = "ID";


            SearchFilter();


        }

        private void button1_Click(object sender, EventArgs e)
        {

            FormServeyAdd fs = new FormServeyAdd(uid);
            fs.ShowDialog();
            SearchFilter();
        }

        private void button2_Click(object sender, EventArgs e)
        {

            int pid = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());
            FormServeyEdit fs = new FormServeyEdit(pid);

            fs.ShowDialog();
            BindingPeopleData();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int pid = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());

            DialogResult dr = MessageBox.Show("Bạn có chắc xóa người này", "Xóa ", MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {

                var pl = (from p in ds.Peoples
                          where p.ID == pid
                          select p).FirstOrDefault();

                ds.Peoples.DeleteOnSubmit(pl);
                ds.SubmitChanges();

                MessageBox.Show("Đã xóa thành công ");
                BindingPeopleData();
            }

        }


        private void search_Click(object sender, EventArgs e)
        {
            SearchFilter();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }



        private void ddlWard_SelectionChangeCommitted(object sender, EventArgs e)
        {
            int wardid = (int)this.ddlWard.SelectedValue;

            List<ItemExtra> ltvillage = new List<ItemExtra>();
            ltvillage.Add(new ItemExtra { ID = -1, Name = "Tất cả" });
            var village = from v in ds.Villages
                          where v.WardID == wardid
                          select new ItemExtra { ID = v.VillageID, Name = v.VillageName };
            ltvillage.AddRange(village.ToList());
            this.ddlVillage.DataSource = ltvillage;
            this.ddlVillage.ValueMember = "ID";
            this.ddlVillage.DisplayMember = "Name";
            this.ddlVillage.SelectedIndex = 0;


            SearchFilter();
        }

        private void ddlGroupCat_SelectionChangeCommitted(object sender, EventArgs e)
        {
            int grcatid = (int)this.ddlGroupCat.SelectedValue;
            //khoi tao đối tượng

            List<ItemExtra> cats = new List<ItemExtra>();
            cats.Add(new ItemExtra { ID = -1, Name = "Tất cả" });
            var cat = from gr in ds.OptionCates
                      where gr.CateDisableID==grcatid
                      select new ItemExtra { ID = gr.ID, Name = gr.DegreeName };

            cats.AddRange(cat.ToList());
            ddlCat.DataSource = cats;
            ddlCat.DisplayMember = "Name";
            ddlCat.ValueMember = "ID";

            SearchFilter();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            
            int wardid = ddlWard.SelectedIndex == -1 ? -1 : (int)ddlWard.SelectedValue;
            int villageid = ddlVillage.SelectedIndex == -1 ? -1 : (int)ddlVillage.SelectedValue;
            int groupcatid = ddlGroupCat.SelectedIndex == -1 ? -1 : (int)ddlGroupCat.SelectedValue;
            int catid = ddlCat.SelectedIndex == -1 ? -1 : (int)ddlCat.SelectedValue;
            int _uid = ddlUser.SelectedIndex == -1 ? -1 : (int)ddlUser.SelectedValue;

            int uidddl = ddlUser.SelectedIndex == -1 ? -1 : (int)ddlUser.SelectedValue;
            int compare = ddlCompareNumber.SelectedIndex == -1 ? -1 : (int)ddlCompareNumber.SelectedValue;
            string fistname = this.txtName.Text.Trim();
            string faminyname = this.txtFamilyName.Text.Trim();

            ReportCenter rc = new ReportCenter();

            rc.ShowMultiple(villageid, wardid, _uid, fistname, faminyname, catid, groupcatid,compare);
            rc.Show();
        }

    }
}
